CtgProveedor = function(){
	var configWindow = {
		add: "ctgProveedorAgregarTop",
		edit: "ctgProveedorEditarTop",
		save: "ctgProveedorGuardarTop",
		remove: "ctgProveedorEliminarTop",
		grid: "ctgProveedorGrid",
		form: "ctgProveedorForm"
	};
	var configWindowBottom = {
			add: "ctgProveedorAgregarBottom",
			edit: "ctgProveedorEditarBottom",
			save: "ctgProveedorGuardarBottom",
			remove: "ctgProveedorEliminarBottom"
	};
	return {
		agregarCtgProveedor: function(){
			Asi.form.switchButton(configWindow, "add");
			Asi.form.switchButton(configWindowBottom, "add");
//			Ext.getCmp("ctgProveedorFechaNacimiento").allowEnable = true;
		},
		editarCtgProveedor: function(){
			Asi.form.switchButton(configWindow, "edit");
			Asi.form.switchButton(configWindowBottom, "edit");
		},
		eliminarCtgProveedor: function(){
			Asi.message.confirmDelete(function(){
				Asi.message.progress("Eliminando Informaci\u00F3n...");
				Ext.Ajax.request({
					timeout: Asi.constants.TIMEOUT_SECONDS,
					url: Asi.constants.CONTEXT_PATH + "/ctgProveedor/delete",
					params: {
						ctgProveedorId: Asi.utils.getValue("ctgProveedorId")
					},
					callback: function(options, success, response){
						Ext.Msg.hide();
						if(success){
							var jsonObject = Asi.utils.ajaxRequestGetJson(response);
							if(jsonObject && jsonObject.success){
								Asi.form.switchButton(configWindow, "remove");
								Asi.form.switchButton(configWindowBottom, "remove");
								Asi.message.alert(jsonObject.message);
								Asi.form.clearAndDisable("ctgProveedorForm");
								if(jsonObject.ctgProveedors){
			    					Ext.getCmp("ctgProveedorGrid").getStore().loadData(jsonObject.ctgProveedors);
								}
								return;
							}
							jsonObject = jsonObject || {};
							Asi.message.alertInvalid(jsonObject.message || Asi.constants.DEFAULT_ERROR_MESSAGE);
							return;
						}
						Asi.message.alertInvalid(Asi.constants.DEFAULT_ERROR_MESSAGE);
					}
				});
			});
		},
		validacionIngresodeNuevoInventario: function(){
			var i = 0;
			var gridCompra = Ext.getCmp("ctgProveedorGrid");
			var gridData = 0;
			Ext.each(gridCompra.getStore().getRange(), function(){
				if(Ext.getCmp("ctgProveedorGrid").getStore().getRange()[i].data.materiaPrimaId == Ext.getCmp("materiaPrimaIdDos").getValue()){
					gridData = 1;
				}
				i++;
          });
			return gridData;
		},
		guardarCtgProveedor: function(){
			
			
			Asi.message.progress("Guardando Informaci\u00F3n...");
			Ext.getCmp("ctgProveedorForm").getForm().submit({
    			url: Asi.constants.CONTEXT_PATH + "/empleado/ctgProveedorSave",
    			timeout: Asi.constants.TIMEOUT_SECONDS,
    			params: {
    				
    			},
    			success: function(form, action){
    				Asi.form.switchButton(configWindow, "save");
    				Asi.form.switchButton(configWindowBottom, "save");
    				Asi.message.alert(action.result.message);
    				Asi.form.setDisable("ctgProveedorForm", true);
    				if(action.result.ctgProveedores){
    					Ext.getCmp("ctgProveedorGrid").getStore().loadData(action.result.ctgProveedores);
    					Ext.getCmp("ctgProveedorGrid").getSelectionModel().select(action.result.ctgProveedorIndex);
    				}
    			},
    			failure: Asi.form.failureProcedure
   			});
			
			
		},
		init: function(config){
			var proveedores = config.proveedores;
			var configToReturn = {};
			configToReturn.items = [];
			configToReturn.menu = AsiBotones.getBotones(AsiGen.getTipoUsuario(), AsiGen.getPantallaId());
			configToReturn.items.push({
				flex: 1,
				title: "PROVEEDORES",
				autoScroll: true,
				xtype: "panel",
				layout: {
				    type: "vbox",
				    align : "center",
				    pack  : "start"
				},
				defaults: {width: 730, margins: "5 0 5 0"},
				dockedItems: [
					{
						
						xtype: "toolbar",
						dock: "top",
//						hidden: AsiKYC.getKycVigente() === false,
						items: [
						   '->',
						   {
							   text: "Nuevo",
							   id: "ctgProveedorAgregarTop",
							   iconCls: Asi.constants.icons.ADD_ICON,
							   handler: CtgProveedor.agregarCtgProveedor
						   },{
					    	   text: "Editar",
					    	   id: "ctgProveedorEditarTop",
							   iconCls: Asi.constants.icons.EDIT_ICON,
							   handler: CtgProveedor.editarCtgProveedor,
					       },{
					    	   text: "Guardar",
					    	   id: "ctgProveedorGuardarTop",
							   iconCls: Asi.constants.icons.SAVE_ICON,
							   handler: CtgProveedor.guardarCtgProveedor
					       },{
					    	   text: "Eliminar",
					    	   id: "ctgProveedorEliminarTop",
							   iconCls: Asi.constants.icons.DELETE_ICON,
							   handler: CtgProveedor.eliminarCtgProveedor
					       }
					    ]
					},{
						
			        	xtype: "toolbar",
			        	dock: "bottom",
//			        	hidden: AsiKYC.getKycVigente() === false,
			        	bodyStyle: "border: solid",
			        	items: [
			        	   '->',
			        	   {
							   text: "Nuevo",
							   id: "ctgProveedorAgregarBottom",
							   iconCls: Asi.constants.icons.ADD_ICON,
							   handler: CtgProveedor.agregarCtgProveedor
						   },{
					    	   text: "Editar",
					    	   id: "ctgProveedorEditarBottom",
							   iconCls: Asi.constants.icons.EDIT_ICON,
							   handler: CtgProveedor.editarCtgProveedor
					       },{
					    	   text: "Guardar",
					    	   id: "ctgProveedorGuardarBottom",
							   iconCls: Asi.constants.icons.SAVE_ICON,
							   handler: CtgProveedor.guardarCtgProveedor
					       },{
					    	   text: "Eliminar",
					    	   id: "ctgProveedorEliminarBottom",
							   iconCls: Asi.constants.icons.DELETE_ICON,
							   handler: CtgProveedor.eliminarCtgProveedor
					       }
		        	    ]
			        }
	            ],
				items: [
					{
						title: "LISTA DE PROVEEDORES",
						xtype: "grid",
						id: "ctgProveedorGrid",
						height: 150,
						minHeight: 150,
						collapsible : true,
						store: new Ext.data.SimpleStore({
					    	data: proveedores || [],
					    	fields: [
								"ctgProveedorId",
								"ctgProveedorNombre",
								"ctgProveedorDireccion",
								"ctgProveedorEmail",
								"ctgProveedorCiudad",
								"ctgProveedorIdentificacion",
								"ctgProveedorTelefono",
								"ctgProveedorNoReg",
								"ctgProveedorEstado"
			    	        ]
					    }),
					    columns: [
							{header: "Id",  dataIndex: "ctgProveedorId", flex:1, width: 120},
				            {header: "Nombre",  dataIndex: "ctgProveedorNombre", flex:1, minWidth: 120},
				            {header: "Cantidad",  dataIndex: "ctgProveedorDireccion", flex:1, width: 120}
					    ],
					    listeners: {
					    	selectionchange: function(model, records){
					    		if(!records || records.length <= 0) return;
					    		var record = records[0];
					    		if(record != null){

					    			Asi.form.setValues("ctgProveedorForm", record.data);
					    			Asi.form.setDisable("ctgProveedorForm");
					    		}
					    		Asi.form.switchButton(configWindow, "rowclick");
					    		Asi.form.switchButton(configWindowBottom, "rowclick");
					    	},
					    	afterrender: function(){
					    		if(this.store.data.items.length > 0) Asi.grid.selectFirstRow(this);
					    		Asi.form.switchButton(configWindow, "cancelNotClear");
					    		Asi.form.switchButton(configWindowBottom, "cancelNotClear");
					    	}
					    }
					},{
			        	xtype: "form",
						id: "ctgProveedorForm",
						flex: 1,
					    border: false,
					    autoScroll: true,
						layout: {
							type: "table",
							columns: 6,
							style : {
								width : "730px",
								"margin-top" : "5px",
								"margin-bottom" : "40px"
							},
							align : "center"
						},
						defaults: {
							width: 230,
							selectOnFocus: true,
							enforceMaxLength: true,
							maxLength: 200,
							typeAhead: true,
							minChars: 1,
							queryMode: "local",
							forceSelection: true,
							allowEnable: true,
							colspan: 2
						},
						items: [
				        	 {	
								xtype: "label",
								text: "ADMINISTRACION DE PROVEEEDORES",
								cls: "x-form-item label_header",
								colspan: 8,
								width: 730
							},
							{xtype: "label", text: "Nombre", cls: "x-form-item label_spacing"},
							{xtype: "label", text: "Direccion", cls: "x-form-item label_spacing"},
							{xtype: "label", text: "Email", cls: "x-form-item label_spacing"},
							{
								xtype: "textfield",
								id: "ctgProveedorNombre",
								name: "ctgProveedor.ctgProveedorNombre"
							},{
								xtype: "textfield",
								id: "ctgProveedorDireccion",
								name: "ctgProveedor.ctgProveedorDireccion"
							},{
								xtype: "textfield",
								id: "ctgProveedorEmail",
								name: "ctgProveedor.ctgProveedorEmail",
								vtype: "email"
							},
							{xtype: "label", text: "Identificacion", cls: "x-form-item label_spacing"},
							{xtype: "label", text: "Telefono", cls: "x-form-item label_spacing"},
							{xtype: "label", text: "No Registro", cls: "x-form-item label_spacing"},
							{
								xtype: "textfield",
								id: "ctgProveedorIdentificacion",
								name: "ctgProveedor.ctgProveedorIdentificacion"
							},{
								xtype: "textfield",
								id: "ctgProveedorTelefono",
								name: "ctgProveedor.ctgProveedorTelefono"
							},{
								xtype: "textfield",
								id: "ctgProveedorNoReg",
								name: "ctgProveedor.ctgProveedorNoReg"
							},
							{xtype: "label", text: "Ciudad", cls: "x-form-item label_spacing"},
							{xtype: "label", text: "Estado", cls: "x-form-item label_spacing"},
							{xtype: "label", text: "", cls: "x-form-item label_spacing"},
							{
								xtype: "textfield",
								id: "ctgProveedorCiudad",
								name: "ctgProveedor.ctgProveedorCiudad"
							},{
								xtype: "combo",
								id: "ctgProveedorEstado",
								name: "ctgProveedor.ctgProveedorEstado",
								colspan: 3,
								store: new Ext.data.SimpleStore({
									data: Asi.combos.yesnoArray() || [],
									fields: [
									         "id",
									         "descripcion"
									        ]
								}),
								displayField: "descripcion",
								valueField: "id",
								allowBlank: false
							},{
								xtype: "hidden",
								id: "ctgProveedorId",
								name: "ctgProveedor.ctgProveedorId"
							}
							
							
							
							
							

						]
			        }
				]
				
			});
			return configToReturn;
		}
	};
}();